<?php

namespace app\automatic\controller\m3688;

use app\spread\controller\Free as Base;
use service\ApiReturn;
use think\Db;


class Freeworkorderlist extends Base
{
    // 定义模块名
    protected $module = '';
    
    public function detail($id = 0)
    {
        $data = input();
        $gongdanbianhao = $data['gongdanbianhao'] ?? '';
        $where = [];
        if ($id != 0) {
            $where[] = ['aid', '=', $id];
        } else if (gongdanbianhao != '' ) {
            $where[] = ['gongdanbianhao', '=', $gongdanbianhao];
        } else {
            $this->error('无效ID');
        }
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        
        // 工单数据
        $data = Db::table('lb_free_work_order_list')->where($where)->find();
        if (!$data) {
            $this->error('工单不存在');
        }
        $gongdanzhuangtai = [
                1 => '待受理',
                2 => '处理中',
                3 => '已完成',
                4 => '不予受理',
                5 => '暂不处理'
            ];
        
        $data['gongdanzhuagntai_name'] = $gongdanzhuangtai[$data['gongdanzhuangtai']] ?? '待受理';
        
        $evallist = Db::table('lb_free_work_order_record')->where('gongdanbianhao', $data['gongdanbianhao'])->order('aid', 'desc')->select();
        $handle_user = "售后工程师";
        $this->assign('handle_user', $handle_user);
        $this->assign('evallist', $evallist);
        $this->assign('data', $data);
        
        return $this->fetch('automatic@m3688/workorderdetail');
        
    }
    
    
    /**
     * 回复
     */
    public function reply()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $member_auth = session('member_auth');
        
            Db::startTrans();
            try {
                
                $add = [
                        'member_id' => MemberID ?? $member_auth['member_id'],
                        'uid' => $member_auth['uid'],
                        'create_time' => time(),
                        'update_time' => time(),
                        'create_id' => $member_auth['uid'],
                        'gongdanbianhao' => $data['gongdanbianhao'],
                        'nickname' => $member_auth['nickname'],
                        'content' => $data['content']
                    ];
                    
                $res = Db::table('lb_free_work_order_record')->insertGetId($add);
                if ($res) {
                    $sendReply = [
                        'member_id' => MemberID ?? $member_auth['member_id'],
                        'company_name' => $member_auth['company_name'],
                        'code' => $data['gongdanbianhao'],
                        'beizhu' => $data['content']
                    ];
                    if (!empty($sendReply)) {
                        $url = 'https://yufabu.saizhuge.net/api/v1/6580ee7aa6efd';
                        $res = curl_post($url, $sendReply);
                        $res = json_decode($res, true);
                        
                        if ($res['code'] != 1) {
            		        throw new \Exception($res['msg'],-1);
            		    }
            		    
            		    Db::commit();
                    }
                    
                    return ApiReturn::r(1, [], '回复成功');
                }
                
            } catch (\Exception $e){
                Db::rollback();
                $msg = $e->getMessage();
                $code = $e->getCode();
                return ApiReturn::r($code, [], $msg);
            }
            
            
        }
    }
    
    
    
    
    
    
    
    
    
}